package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.StoragesMapper;
import cn.wolfcode.crm.query.AccidentQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
@Service
public class AccidentServiceImpl implements IAccidentService{
    @Autowired
    private AccidentMapper mapper;
    @Autowired
    private StoragesMapper storagesMapper;

    public void save(Accident entity) {
        //根据保险单号获取客户id
        Long clientId = storagesMapper.getClientId(entity.getInsuranceNo());
        //设置状态为待验证状态 state = 1
        Client client = new Client();
        client.setId(clientId);
        entity.setClient(client);
        entity.setState(Accident.STATE_INVALIDATE);
        entity.setReportTime(new Date());
        //整合后,将登陆当前账号用户当做录单员工存入数据表中
        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setInputStaff(emp.getUsername());
        mapper.insertForAccident(entity);
    }

    public void update(Accident entity) {
        mapper.updateByPrimaryKeyForAccident(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public Accident get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Accident> getAll() {
        return mapper.selectAll();
    }

    public PageResult query(AccidentQueryObject qo) {
        int count = mapper.queryForCount(qo);
        if(count==0){
            return new PageResult(0, Collections.emptyList());
        }
        List<Accident> list = mapper.queryForList(qo);
        return new PageResult(count,list);
    }
    /*
     * 查询所有验证待理赔数据
     * */
    public List<Accident> accidentAndClaimBill() {
        return mapper.accidentAndClaimBill();
    }

    /*
     * 更改理赔表单状态为已验证,待填理赔状态 state=4
     * */
    public void updateState(Long id) {
        int state = Accident.STATE_INVALIDATED_BILL;
        mapper.updateStateNoUser(id,state);
    }
    /*
    * 将数据状态更改为:理赔明细已填,待提审 state=3
    * */
    public void updateBilledAudit(Long accidentId) {
        int state = Accident.STATE_BILLED_AUDIT;
        mapper.updateStateNoUser(accidentId,state);
    }
    /*
     * 更改数据状态为已验证,待填理赔明细状态 state=2
     * */
    public void updateInvalidateState(Long id) {
        int state = Accident.STATE_INVALIDATED_BILL;
        //整合后将当前用户设置为验证员工
        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();
        String validateStaff=emp.getUsername();
        mapper.updateState(id,state,validateStaff);
    }
    /*
     * 更改数据状态为已审核,待支付状态 state=5
     * */
    public void changeStateAudited(Long id) {
        int state = Accident.STATE_AUDIT_PAY;
        mapper.updateStateNoUser(id,state);
    }
    /*
     * 更改数据状态为已支付 state=6
     * */
    public void updateStateToPaied(Long id) {
        int state = Accident.STATE_PAYED;
        mapper.updateStateNoUser(id,state);
    }
    /*
    * 查询所有状态的数据
    * */
    public PageResult allList(AccidentQueryObject qo) {
        int count = mapper.queryForAllCount(qo);
        if(count==0){
            return new PageResult(0, Collections.emptyList());
        }
        List<Accident> list = mapper.queryForAllList(qo);
        return new PageResult(count,list);
    }

    /*
    * 查询所有状态为state=4 待审核的数据
    * */
    public PageResult listByAudit(QueryObject qo) {
        int count = mapper.queryByAuditForCount(qo);
        if(count==0){
            return new PageResult(0, Collections.emptyList());
        }
        List<Accident> list = mapper.queryByAuditForList(qo);
        return new PageResult(count,list);
    }
    /*
   * 查询状态state=2和state=3的数据信息,即验证完成,未填明细和已填明细,未提审的数据
   * */
    public PageResult queryValidateUnBill(QueryObject qo) {
        int count = mapper.queryByUnBillForCount(qo);
        if(count==0){
            return new PageResult(0, Collections.emptyList());
        }
        List<Accident> list = mapper.queryByUnBillForList(qo);
        return new PageResult(count,list);
    }
    /*
   * 查询所有状态为state=5 已审核,待支付的数据
   * */
    public PageResult listByCompensation(QueryObject qo) {
        int count = mapper.queryByUnPayForCount(qo);
        if(count==0){
            return new PageResult(0, Collections.emptyList());
        }
        List<Accident> list = mapper.queryByUnPayForList(qo);
        return new PageResult(count,list);
    }


}
